using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
using Pheonix.CodeLibrary.Web;

public partial class ModalPopup : BaseControl
{
    private ITemplate _modalTemplate = null;
    private string _theme = String.Empty;
    private bool _dragable = false;
    private bool _closable = false;

    [TemplateContainer(typeof(ContentContainer))]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    [Browsable(false)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public ITemplate ModalTemplate
    {
        get { return _modalTemplate; }
        set { _modalTemplate = value; }
    }

    public string Theme
    {
        get { return _theme; }
        set { _theme = value; }
    }

    public bool Dragable
    {
        get { return _dragable; }
        set { _dragable = value; }
    }

    public bool Closable
    {
        get { return _closable; }
        set { _closable = value; }
    }

    protected void Page_Init()
    {
        if (_modalTemplate != null)
        {
            ContentContainer container = new ContentContainer();
            _modalTemplate.InstantiateIn(container);
            content.Controls.Add(container);

            // Adding the stylesheet for this control [note: path from page]
            HtmlLink cssLink = new HtmlLink();
            cssLink.Href = "~/control/modal_popup/themes/" + _theme + "/modal_popup.css";
            cssLink.Attributes.Add("rel", "Stylesheet");
            cssLink.Attributes.Add("type", "text/css");
            Page.Header.Controls.Add(cssLink);

            // Adding required javascript for this control [note: path from project root]
            HtmlGenericControl javaScriptLink = new HtmlGenericControl("script");
            javaScriptLink.Attributes.Add("src", "control/modal_popup/scripts/modal_popup.js");
            javaScriptLink.Attributes.Add("type", "text/javascript");
            Page.Header.Controls.Add(javaScriptLink);

            if (_dragable || _closable)
            {
                HtmlGenericControl javaScriptLink4 = new HtmlGenericControl("script");
                javaScriptLink4.Attributes.Add("src", "control/modal_popup/scripts/prototype.js");
                javaScriptLink4.Attributes.Add("type", "text/javascript");
                Page.Header.Controls.Add(javaScriptLink4);

            }

            if (_dragable)
            {                
                HtmlGenericControl javaScriptLink5 = new HtmlGenericControl("script");
                javaScriptLink5.Attributes.Add("src", "control/modal_popup/scripts/scriptaculous.js");
                javaScriptLink5.Attributes.Add("type", "text/javascript");
                Page.Header.Controls.Add(javaScriptLink5);

                HtmlGenericControl javaScriptLink2 = new HtmlGenericControl("script");
                javaScriptLink2.Attributes.Add("src", "control/modal_popup/scripts/dragdrop.js");
                javaScriptLink2.Attributes.Add("type", "text/javascript");
                Page.Header.Controls.Add(javaScriptLink2);

                HtmlGenericControl javaScriptLink3 = new HtmlGenericControl("script");
                javaScriptLink3.Attributes.Add("src", "control/modal_popup/scripts/effects.js");
                javaScriptLink3.Attributes.Add("type", "text/javascript");
                Page.Header.Controls.Add(javaScriptLink3);

                //HtmlGenericControl javaScript = new HtmlGenericControl("script");
                //javaScript.Attributes.Add("type", "text/javascript");
                //javaScript.InnerText = "new Draggable('" + dragableArea.ClientID + "',{ starteffect:function(){ new Effect.Highlight('" + dragableArea.ClientID + "',{queue:'end'}); } }); ";
                //Page.Header.Controls.Add(javaScript);

                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "drage: " + 
                    dragableArea.ClientID, "<script type='text/javascript'>new Draggable('" + 
                    dragableArea.ClientID + "',{ starteffect:function(){ new Effect.Highlight('" + 
                    dragableArea.ClientID + "',{queue:'end'}); } }); </script>");
            }

            if (_closable)
            {
                closeButton.Src = "~/control/modal_popup/themes/" + _theme + "/images/close.jpg";
                closeButton.Attributes.Add("onclick","Element.hide('" + dragableArea.ClientID + "');");
            }
        }
    }

    public override Control FindControl(string id)
    {
        return base.FindControl("content").Controls[0].FindControl(id);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!_closable)
            closeButton.Visible = false;
    }  
}

public class ContentContainer : Control, INamingContainer
{
    internal ContentContainer() { }
}
